package cn.b.sky.mes.qm.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.qm.model.ResponsibilityType;
import cn.b.sky.mes.qm.model.condition.ResponsibilityTypeCondition;
import cn.b.sky.mes.qm.service.ResponsibilityTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : ResponsibilityTypeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.controller<br></strong>
 * <strong>Description : </strong>责任类型控制器<br> 
 * <strong>Create on : 2015年12月9日 上午10:58:28<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/responsibilityType/*")
public class ResponsibilityTypeController extends SecurityController {

  @Resource
  ResponsibilityTypeService responsibilityTypeService;

  @RequestMapping("responsibilityType_list")
  public String selectResponsibilityTypeListByCondition(
          @ModelAttribute("condition") ResponsibilityTypeCondition condition, String navTabId, Model model) {
    condition.setSiteCode(getSiteCode());
    List<ResponsibilityType> list = responsibilityTypeService.selectResponsibilityTypeListByCondition(condition);
    int count = responsibilityTypeService.selectResponsibilityTypeCountByCondition(condition);

    condition.setTotalCount(count);
    condition.setData(list);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "qm/responsibilityType/responsibilityType_list";
  }

  @RequestMapping("addResponsibilityType")
  public String addResponsibilityType(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");

    return "qm/responsibilityType/responsibilityType_add";
  }

  @RequestMapping("modifyResponsibilityType")
  public String modifyResponsibilityType(String id, String navTabId, Model model) {
    ResponsibilityType level = responsibilityTypeService.getResponsibilityType(getSiteCode(), id);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("responsibilityType", level);
    model.addAttribute("pattern", "modify");

    return "qm/responsibilityType/responsibilityType_add";
  }

  @RequestMapping("viewResponsibilityType")
  public String viewResponsibilityType(String id, String navTabId, Model model) {
    ResponsibilityType level = responsibilityTypeService.getResponsibilityType(getSiteCode(), id);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("responsibilityType", level);
    model.addAttribute("pattern", "view");

    return "qm/responsibilityType/responsibilityType_add";
  }

  @RequestMapping("saveResponsibilityType")
  @ResponseBody
  public DWZResult saveResponsibilityType(String id, ResponsibilityType level, String navTabId, Model model) {
    if (level == null || StringUtils.isBlank(level.getResponsibilityTypeCode())) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    level.setSiteCode(getSiteCode());
    setCreateInfo(level);
    if (level.getLockFlag() == null) {
      level.setLockFlag(0);
    }
    if (StringUtils.isBlank(id)) {
      responsibilityTypeService.insertResponsibilityType(level);
    } else {
      responsibilityTypeService.updateResponsibilityType(level);
    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);
  }

  @RequestMapping("deleteResponsibilityType")
  @ResponseBody
  public DWZResult deleteResponsibilityType(String navTabId, Model model, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    String[] idArr = ids.split(",");
    BatchJobReport[] reports = new BatchJobReport[idArr.length];
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      reports[i] = responsibilityTypeService.deleteResponsibilityType(getSiteCode(), idArr[i]);
      if (!reports[i].isSuccess()) {
        failures += 1;
      }
    }

    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("validateResponsibilityTypeCode")
  @ResponseBody
  public boolean validateResponsibilityTypeCode(String responsibilityTypeCode) {
    if (StringUtils.isBlank(responsibilityTypeCode)) {
      return true;
    } else {
      ResponsibilityType level = responsibilityTypeService.getResponsibilityType(getSiteCode(), responsibilityTypeCode);
      return level == null;
    }
  }

  @RequestMapping("responsibilityTypeDropdown")
  @ResponseBody
  public List<ResponsibilityType> responsibilityTypeDropdown(String responsibilityTypeCode) {
    ResponsibilityTypeCondition condition = new ResponsibilityTypeCondition();
    condition.setSiteCode(getSiteCode());
    condition.setResponsibilityTypeCode(responsibilityTypeCode);
    condition.setLockFlag(0);
    return responsibilityTypeService.selectResponsibilityTypeForSuggest(condition);
  }

  @RequestMapping("checkResponsibilityTypeCode")
  @ResponseBody
  public boolean checkResponsibilityTypeCode(String responsibilityTypeCode) {
    ResponsibilityType responsibilityType = responsibilityTypeService.getResponsibilityType(getSiteCode(),
        responsibilityTypeCode);
    return responsibilityType != null && responsibilityType.getLockFlag() != 1;
  }

  @RequestMapping("responsibilityTypeLookup")
  public String responsibilityTypeLookup(@ModelAttribute("condition") ResponsibilityTypeCondition condition,
      String navTabId, Model model) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<ResponsibilityType> list = responsibilityTypeService.selectResponsibilityTypeListByCondition(condition);
    int count = responsibilityTypeService.selectResponsibilityTypeCountByCondition(condition);

    condition.setTotalCount(count);
    condition.setData(list);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "qm/responsibilityType/responsibilityType_lookup";
  }

}